﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AMS.UI.Forms;
using AMS.DataAccess.Trademark.Interfaces;
using AMS.DataAccess.Trademark.BO;
using AMS.Common;
using AMS.ComponentModel;

namespace AMS.Trademark.Management
{
    /// <summary>
    /// 编辑、添加任务对话框
    /// </summary>
    public partial class EditTaskDlg : BaseForm, IEditDialog<TaskCategoryInfo>
    {
        /// <summary>
        /// 编辑、添加任务对话框
        /// </summary>
        public EditTaskDlg()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        public string TaskIndex { get; private set; }
        private BusinessAction action;
        private TaskCategoryInfo task;

        /// <summary>
        /// 编辑任务
        /// </summary>
        /// <param name="rfIndex"></param>
        /// <returns></returns>
        public DialogResult Edit(string rfIndex)
        {
            TaskIndex = rfIndex;
            task = taskDetail.DataBind(rfIndex) as TaskCategoryInfo;
            if (task == null)
            {
                MessageBox.Show("任务不存在，请刷新后重试");
                this.DialogResult = DialogResult.Cancel;
                Close();
                return this.DialogResult;
            }

            Text = string.Format("编辑任务 - {0}", rfIndex);
            action = BusinessAction.Edit;
            taskDetail.SetEditStatus();
            return ShowDialog();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (action == BusinessAction.View)
            {
                MessageBox.Show("请使用 Edit 或 NewRecvFile 接口，而不要直接使用ShowDialog");
                DialogResult = System.Windows.Forms.DialogResult.Cancel;
                return;
            }

            if (!taskDetail.CheckData())
            {
                return;
            }

            IRFTaskManager rftask = ObjectUtility.GetObject<IRFTaskManager>("RFTask");
            if (action == BusinessAction.New)
            {
                task = rftask.GetTask(taskDetail.TaskIndex);
                if (task != null)
                {
                    MessageBox.Show("任务编号已经存在，请重新输入");
                    return;
                }

                // 添加任务
                task = new TaskCategoryInfo();
                taskDetail.UpdateTask(task);
                rftask.AddTask(task);
                MessageBox.Show("添加任务信息成功");
                DialogResult = System.Windows.Forms.DialogResult.OK;
                Close();
            }
            else
            {
                task = new TaskCategoryInfo();
                taskDetail.UpdateTask(task);
                if (rftask.UpdateTask(task))
                {
                    DialogResult = System.Windows.Forms.DialogResult.OK;
                    Close();
                }
                else
                {
                    MessageBox.Show("更新任务信息失败，请刷新后再编辑");
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        public DialogResult EditModel(TaskCategoryInfo task)
        {
            TaskIndex = task.TaskIndex;
            taskDetail.DataBind(task);

            Text = string.Format("编辑任务 - {0}", TaskIndex);
            action = BusinessAction.Edit;
            taskDetail.SetEditStatus();
            return ShowDialog();
        }

        /// <summary>
        /// 新建任务
        /// </summary>
        /// <returns></returns>
        public DialogResult NewModel()
        {
            action = BusinessAction.New;
            Text = "添加任务";
            taskDetail.SetNewStatus();
            return ShowDialog();
        }
    }
}
